import requests
import random
from lxml import etree

if __name__ == "__main__":
    # 不同浏览器的UA
    header_list = [
        # 遨游
        {"user-agent": "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)"},
        # 火狐
        {"user-agent": "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"},
        # 谷歌
        {
            "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11"}
    ]
    header = random.choice(header_list)

    # 不同的代理IP
    proxy_list = [
        {"http": "112.115.57.20:3128"},
        {'http': '121.41.171.223:3128'}
    ]
    proxy = random.choice(proxy_list)
    proxy = None

    url = 'http://www.baidu.com/s?ie=UTF-8&wd=ip'
    # 设置代理
    # 当没有proxies参数时，就是用的自己的IP
    response = requests.get(url=url, headers=header, proxies=proxy)

    # text=文本数据
    # content=二进制数据
    root = etree.HTML(response.text)
    get_text = root.xpath('//*[@id="su"]/@value')[0]
    print(get_text)
